//
//  CMDBUserInfoManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBUserInfoManager: NSObject {
    static let getInstance: CMDBUserInfoManager = CMDBUserInfoManager()
    
    private override init() {
        
    }
    
    var userInfo: CMDBUserInfoBean?
}
extension CMDBUserInfoManager: CMDBUserInfoManagerDelegate {
    
    func queryUserInfo(userInfoParam: CMDBUserInfoParam,handler: CMDBUserInfoHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(userInfoParam.URLString(), param: nil, success: { [weak self] (sysTime, data, page) in
            
            let userInfo = CMDBUserInfoBean.objectWithKeyValues(data as! NSDictionary) as! CMDBUserInfoBean
            
            self!.saveUserInfo(userInfo, identifier: userInfo.uid)
            
            handler(userInfo: userInfo)
            
            }, error: error, failure: failure)
    }
    func modifyUserInfo(modifyInfoParam: CMDBModifyUserInfoParam, success: CMDBModifyUserInfoHandler,error: CMDBHttpErrorHandler,failure: CMDBHttpFailureHandler) throws {
        
        CMDBHttpManager.getInstance.request(modifyInfoParam.URLString(), param: modifyInfoParam.keyValues, success: { (sysTime, data, page) in
            
            success(isSuccess: true,type: modifyInfoParam.modifyType )
            
            }, error: error, failure: failure)
    }
}
// MARK: 用户信息 缓存管理
extension CMDBUserInfoManager {
    
    // 这个在 上面那个网络请求成功后调用
    func saveUserInfo(userInfo: CMDBUserInfoBean, identifier: String) {
        
        CMDBCacheDataManager.getInstance.saveDictionary(userInfo.keyValues, identifier: userInfo.uid)
        
        self.userInfo = userInfo
        
    }
    // 这个在 用户详情vc 销毁之后调用
    func saveUserInfo(identifier: String) {
        
        CMDBCacheDataManager.getInstance.saveDictionary(userInfo!.keyValues, identifier: userInfo!.uid)
        
    }
    func queryUserInfoCache(identifier: String) throws -> CMDBUserInfoBean {
        
        guard let cache = CMDBCacheDataManager.getInstance.queryData(identifier) else {
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.UserInfoCacheEmpty)
        }
        
        let userInfo = CMDBUserInfoBean.objectWithKeyValues(cache) as! CMDBUserInfoBean
        
        self.userInfo = userInfo
        
        return userInfo
    }
}
// MARK: 获取密码验证token

extension CMDBUserInfoManager {
    
    func vertifyPassword(verifyPwdParam: CMDBVerifyPasswordParam, handler: CMDBVerifyPwdHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard verifyPwdParam.password.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        
        CMDBHttpManager.getInstance.request(verifyPwdParam.URLString(), param: verifyPwdParam.keyValues, success: { (sysTime, data, page) in
            
            let verifyPwdBean = CMDBVerifyPwdBean.objectWithKeyValues(data as! NSDictionary) as! CMDBVerifyPwdBean
            
            handler(verifyPwdBean: verifyPwdBean)
            
            }, error: error, failure: failure)
        
    }
}

// MARK: 绑定手机号
extension CMDBUserInfoManager {
    
    func bindPhone(bindPhoneParam: CMDBBindPhoneParam, handler: CMDBModifyUserInfoHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard bindPhoneParam.phone.validPhoneNumber() else {
            
            printLog(CMDBUserControllerExceptionCodeType.PhoneInvalid.rawValue)
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid)
        }
        guard bindPhoneParam.vcode.cmdb_length == VCode_Length else {
            
            printLog(CMDBUserControllerExceptionCodeType.VCodeInvalid.rawValue)
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeInvalid)
        }
        
        CMDBHttpManager.getInstance.request(bindPhoneParam.URLString(), param: bindPhoneParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess: true,type: .Phone)
            
            }, error: error, failure: failure)
        
    }
}
// MARK: 绑定邮箱
extension CMDBUserInfoManager {
    
    func bindEmail(bindEamilParam: CMDBBindEmailParam, handler: CMDBModifyUserInfoHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard bindEamilParam.email.validEmail() else {
            
            printLog(CMDBUserControllerExceptionCodeType.EmailInvalid.rawValue)
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.EmailInvalid)
        }
        
        CMDBHttpManager.getInstance.request(bindEamilParam.URLString(), param: bindEamilParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess: true,type: .Email)
            
            }, error: error, failure: failure)
        
    }
}


// MARK: 获取粉丝列表
extension CMDBUserInfoManager {
    
    func queryFans(fansParam: CMDBFansListParam, tableView: UITableView, handler: CMDBFansListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(fansParam.URLString(), param: fansParam.keyValues, ownPage: fansParam.page, tableView: tableView, success: { [weak self] (sysTime, data, page) in
            
            let arr: Array<CMDBUserInfoBean>? = CMDBUserInfoBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBUserInfoBean>
            
            handler(fansList: arr)
            
            //            if let _ = arr {
            //
            //                self!.saveFavorMeList(arr!, identifier: "FavorMeList_\(CMDBApplicationManager.getInstance.account!.uid)")
            //            }
            
            }, error: error, failure: failure)
        
    }
}
// MARK: 获取好友列表
extension CMDBUserInfoManager {
    
    func queryFriends(friendsParam: CMDBFriendsListParam, tableView: UITableView, handler: CMDBFriedsListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        
        CMDBHttpManager.getInstance.request(friendsParam.URLString(), param: friendsParam.keyValues, ownPage: friendsParam.page, tableView: tableView, success: { [weak self] (sysTime, data, page) in
            
            let arr: Array<CMDBUserInfoBean>? = CMDBUserInfoBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBUserInfoBean>
            
            handler(friendsList: arr)
            
            //            if let _ = arr {
            //
            //                self!.saveFavorMeList(arr!, identifier: "FavorMeList_\(CMDBApplicationManager.getInstance.account!.uid)")
            //            }
            
            }, error: error, failure: failure)
    }
}
// MARK: 获取关注的用户列表
extension CMDBUserInfoManager {
    
    func queryFollows(followsParam: CMDBQueryFollowsParam, tableView: UITableView, handler: CMDBFollowsListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(followsParam.URLString(), param: followsParam.keyValues, ownPage: followsParam.page, tableView: tableView, success: { [weak self] (sysTime, data, page) in
            
            let arr: Array<CMDBUserInfoBean>? = CMDBUserInfoBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBUserInfoBean>
            
            handler(followsList: arr)
            
            //            if let _ = arr {
            //
            //                self!.saveFavorMeList(arr!, identifier: "FavorMeList_\(CMDBApplicationManager.getInstance.account!.uid)")
            //            }
            
            }, error: error, failure: failure)
        
    }
    //
}

extension CMDBUserInfoManager {
    
    func queryLanguages(languageParam: CMDBLanguageParam, handler: CMDBLanguageListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(languageParam.URLString(), param: languageParam.keyValues, success: { (sysTime, data, page) in
            
            let arr: Array<CMDBLanguageBean>? = CMDBLanguageBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBLanguageBean>
            
            handler(languageList: arr)
            
            //            if let _ = arr {
            //
            //                self!.saveFavorMeList(arr!, identifier: "FavorMeList_\(CMDBApplicationManager.getInstance.account!.uid)")
            //            }
            
            }, error: error, failure: failure)
    }
}